/*
 *  Copright: 2008 Philipp Leitner, Distributed Systems Group,
 *            TU Wien, leitner@infosys.tuwien.ac.at
 *
 *  This file is part of DAIOS.
 *
 *  DAIOS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation.
 * 
 *  DAIOS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with DAIOS.  If not, see
 *  <http://www.gnu.org/licenses/>.
 * 
 */

package at.ac.tuwien.infosys.dsg.daios.wsdl.parser;

import java.net.URL;

import javax.wsdl.Definition;

import org.apache.xmlbeans.SchemaTypeSystem;

import at.ac.tuwien.infosys.dsg.daios.framework.interfaces.IConstants;
import at.ac.tuwien.infosys.dsg.daios.wsdl.parser.soapui.SchemaException;
import at.ac.tuwien.infosys.dsg.daios.wsdl.parser.soapui.SchemaUtils;
import at.ac.tuwien.infosys.dsg.daios.wsdl.parser.soapui.SoapVersion;
import at.ac.tuwien.infosys.dsg.daios.wsdl.parser.soapui.UrlWsdlLoader;
import at.ac.tuwien.infosys.dsg.daios.wsdl.typesystem.WSDLTypeTree;

/**
 * This is the conrete type parser used by Daios. It uses code extracts
 * from the open source project soapUI (see the package
 * at.ac.tuwien.infosys.dsg.daios.wsdl.parser.soapui) to parse the complex
 * types contained within a WSDL definition.
 * 
 * @author philipp
 *
 */
public class SOAPUIParser extends TypeParser {

	/**
	 * Construct the type tree from a  given WSDL definition.
	 * 
	 * <p>
	 * 
	 * Internally code from soapUI is used to do the actual
	 * type parsing.
	 * 
	 * @param wsdl The WSDL definition.
	 * @param location The URL where the WSDL can be found. This
	 * is necessary so that relative URLs can be followed by the
	 * parser.
	 * @param style The invocation style to use. Possible values
	 * are 'document' and 'rpc'.
	 * @return The entirely parsed type tree.
	 */
	public WSDLTypeTree getElementTypes(Definition wsdl, URL location, String style) {
		
	     try {
	    	 SchemaTypeSystem schemaTypes =
	    		 SchemaUtils.loadSchemaTypes(location.toExternalForm(), SoapVersion.Soap11, new
	    			UrlWsdlLoader(location.toExternalForm()));	
	    	 
	    	 if(style.equals(IConstants.RPC_STYLE))
	    		 return WSDLTypeTree.parseComplexTypeTree(schemaTypes);
	    	 else
	    		 return WSDLTypeTree.parseElementTypeTree(schemaTypes);
	     
	     } catch (SchemaException e) {
	    	 log.severe("There was a fatal error while parsing the "+
	    		"WSDL XML Schema.");
			e.printStackTrace();
			return null;
		}
		
	}

}
